﻿//Author: Alexis
//Date: 20110918

using System;
using System.Net;
using Hammock;
using Hammock.Web;

namespace vDisk.WindowsPhone
{
    public static class RequestHelper
    {
        internal static UserLogin login = null;

        internal static RestRequest GetRequest(string action, string method = "")
        {
            var request = new RestRequest();
            request.Method = WebMethod.Get;
            if (!string.IsNullOrEmpty(method))
            {
                request.AddParameter("m", method);
            }
            request.AddParameter("a", action);
            request.AddParameter("token", login.Token);
            request.AddParameter("dologid", login.DoLogid);
            return request;
        }

        /// <summary>
        /// get 保持同步 request
        /// </summary>
        /// <returns></returns>
        public static RestRequest GetKeepRequest()
        {
            return GetRequest("keep");
        }

        /// <summary>
        /// get 获得token request
        /// </summary>
        /// <param name="email">账户</param>
        /// <param name="password">密码</param>
        /// <param name="loginType">登录类型。sinat：新浪微博帐号，local:微盘帐号</param>
        /// <returns></returns>
        public static RestRequest GetTokenRequest(string email, string password, string loginType = "sinat")
        {
            var request = new RestRequest();
            request.Method = WebMethod.Post;
            request.AddParameter("m", "auth");
            request.AddParameter("a", "get_token");
            request.AddParameter("account", email);
            request.AddParameter("password", password);
            request.AddParameter("appkey", Constants.AppKey);
            request.AddParameter("time", Utils.GetTime());
            request.AddParameter("app_type", loginType);
            string sss = "account=" + email + "&appkey=" + Constants.AppKey + "&password=" + password + "&time=" + Utils.GetTime();
            request.AddParameter("signature", Utils.GetSignature(sss, Constants.AppSecret));
            return request;
        }

        /// <summary>
        /// 保持token
        /// 说明: 空闲时(不做任何操作, 15分钟后)token会失效, 所以需要保持token, 10-15分钟执行一次
        /// </summary>
        /// <returns></returns>
        public static RestRequest GetKeepTokenRequest()
        {
            return GetRequest("keep_token", "user");
        }

        /// <summary>
        /// get 文件上传(10M以下) request
        /// </summary>
        ///<param name="upload">上传类信息</param>
        ///<param name="isShare">是否并分享</param>
        /// <returns></returns>
        public static RestRequest GetUploadFileRequest(UploadFileReq upload, bool isShare = false)
        {
            var request = new RestRequest();
            request.Method = WebMethod.Post;
            request.AddField("m", "file");
            if (!isShare)
            {
                request.AddField("a", "upload_file");
            }
            else
            {
                request.AddField("a", "upload_share_file");
            }
            request.AddField("token", login.Token);
            request.AddField("dir_id", upload.DirId);
            if (string.IsNullOrEmpty(upload.IsCover))
            {
                request.AddField("cover", "yes");
            }
            else
            {
                request.AddField("cover", upload.IsCover);
            }
            request.AddFile(upload.FileName, upload.FileName, upload.SourceFolder);
            request.AddParameter("file", upload.FileName);
            request.AddParameter("dologid", login.DoLogid);
            return request;
        }

        /// <summary>
        /// 上传文件并分享(10M以下)
        /// </summary>
        /// <param name="upload"></param>
        /// <returns></returns>
        public static RestRequest GetUploadShareFileRequest(UploadFileReq upload)
        {
            return GetUploadFileRequest(upload, true);
        }

        /// <summary>
        /// get 创建目录 request
        /// </summary>
        /// <param name="dirName">目录的名称</param>
        /// <param name="parentDir">父目录名称, 如果是根目录值为: ""</param>
        /// <param name="parentId">父目录的id, 如果是根目录值为: 0</param>
        /// <returns></returns>
        public static RestRequest GetCreateDirRequest(string dirName, string parentDir, string parentId)
        {
            var request = new RestRequest { Method = WebMethod.Post };
            request.AddField("m", "dir");
            request.AddField("a", "create_dir");
            request.AddField("token", login.Token);
            request.AddField("dologid", login.DoLogid);
            request.AddParameter("create_name", dirName);
            request.AddParameter("parent_name", parentDir);
            request.AddParameter("parent_id", parentId);
            return request;
        }

        /// <summary>
        /// get 获得列表 request
        /// </summary>
        /// <param name="page">当前页码</param>
        /// <param name="pageSize">页中项目数</param>
        /// <param name="dir_id">目录id</param>
        /// <returns></returns>
        public static RestRequest GetListRequest(string page,string pageSize, string dir_id)
        {
            var request = new RestRequest { Method = WebMethod.Post };
            request.AddField("m", "dir");
            request.AddField("a", "getlist");
            request.AddField("token", login.Token);
            request.AddField("dologid", login.DoLogid);
            request.AddParameter("page", page);
            request.AddParameter("pageSize", pageSize);
            request.AddParameter("dir_id", dir_id);
            return request;
        }

        /// <summary>
        /// get 获得容量信息 request
        /// </summary>
        /// <returns></returns>
        public static RestRequest GetQuotaRequest()
        {
            return GetRequest("get_quota", "file");
        }

        /// <summary>
        /// get 无文件上传(sha1) request
        /// </summary>
        /// <param name="sha1"></param>
        /// <param name="file_name"></param>
        /// <returns></returns>
        public static RestRequest GetUploadWithSha1Request(string sha1, string file_name)
        {
            var request = new RestRequest { Method = WebMethod.Post };
            request.AddField("m", "file");
            request.AddField("a", "upload_with_sha1");
            request.AddField("token", login.Token);
            request.AddField("dologid", login.DoLogid);
            request.AddParameter("sha1", sha1);//文件的sha1值
            request.AddParameter("file_name", file_name);
            return request;
        }

        /// <summary>
        /// get 获得单个文件信息 request
        /// </summary>
        /// <param name="fid">文件id</param>
        /// <returns></returns>
        public static RestRequest GetFileInfoRequest(string fid)
        {
            var request = new RestRequest { Method = WebMethod.Post };
            request.AddField("m", "file");
            request.AddField("a", "get_file_info");
            request.AddField("token", login.Token);
            request.AddField("dologid", login.DoLogid);
            request.AddParameter("fid", fid);
            return request;
        }

        /// <summary>
        /// get 删除目录 request
        /// </summary>
        /// <param name="dir_id">目录id</param>
        /// <returns></returns>
        public static RestRequest GetDeleteDirRequest(string dir_id)
        {
            var request = new RestRequest { Method = WebMethod.Post };
            request.AddField("m", "dir");
            request.AddField("a", "delete_dir");
            request.AddField("token", login.Token);
            request.AddField("dologid", login.DoLogid);
            request.AddParameter("dir_id", dir_id);
            return request;
        }

        /// <summary>
        /// get 删除文件 request
        /// </summary>
        /// <param name="fid">文件</param>
        /// <returns></returns>
        public static RestRequest GetDeleteFileRequest(string fid)
        {
            var request = new RestRequest { Method = WebMethod.Post };
            request.AddField("m", "file");
            request.AddField("a", "delete_file");
            request.AddField("token", login.Token);
            request.AddField("dologid", login.DoLogid);
            request.AddParameter("dir_id", fid);
            return request;
        }

        /// <summary>
        /// get 复制文件 request 
        /// </summary>
        /// <param name="fid">文件id</param>
        /// <param name="new_name">副本的名称</param>
        /// <param name="to_dir_id">目标目录的id, 根目录值为: 0</param>
        /// <returns></returns>
        public static RestRequest GetCopyFileRequest(string fid, string new_name, string to_dir_id)
        {
            return GetHandleFileRequest("copy_file", fid, new_name, to_dir_id);
        }

        /// <summary>
        /// get 移动文件 request
        /// </summary>
        /// <param name="fid">文件id</param>
        /// <param name="new_name">移动后的文件名</param>
        /// <param name="to_dir_id">目标目录的id, 根目录值为: 0</param>
        /// <returns></returns>
        public static RestRequest GetMoveFileRequest(string fid, string new_name, string to_dir_id)
        {
            return GetHandleFileRequest("move_file", fid, new_name, to_dir_id);
        }

        /// <summary>
        /// get 文件重命名 request
        /// </summary>
        /// <param name="fid">文件id</param>
        /// <param name="new_name">新文件名</param>
        /// <returns></returns>
        public static RestRequest GetRenameFileRequest(string fid, string new_name)
        {
            return GetHandleFileRequest("rename_file", fid, new_name);
        }

        internal static RestRequest GetHandleFileRequest(string action, string fid, string new_name, string to_dir_id = "")
        {
            var request = new RestRequest { Method = WebMethod.Post };
            request.AddField("m", "file");
            request.AddField("a", action);
            request.AddField("token", login.Token);
            request.AddField("dologid", login.DoLogid);
            request.AddParameter("fid", fid);
            request.AddParameter("new_name", new_name);
            if (!string.IsNullOrEmpty(to_dir_id))
            {
                request.AddParameter("to_dir_id", to_dir_id);
            }
            return request;
        }

        /// <summary>
        /// get 目录重命名 request
        /// </summary>
        /// <param name="dir_id">目录id(不能为0)</param>
        /// <param name="new_name">新名称</param>
        /// <returns></returns>
        public static RestRequest GetRenameDirRequest(string dir_id, string new_name)
        {
            return GetHandleFileRequest("rename_dir", dir_id, new_name);
        }

        /// <summary>
        /// get 移动目录 request
        /// </summary>
        /// <param name="dir_id">目录id(不能为0)</param>
        /// <param name="new_name">新名称</param>
        /// <param name="to_parent_id">目标父目录id, 根目录值为: 0</param>
        /// <returns></returns>
        public static RestRequest GetMoveDirRequest(string dir_id, string new_name, string to_parent_id)
        {
            return GetHandleFileRequest("move_dir", dir_id, new_name, to_parent_id);
        }

        internal static RestRequest GetHandleDirRequest(string action, string dir_id, string new_name, string to_parent_id = "")
        {
            var request = new RestRequest { Method = WebMethod.Post };
            request.AddField("m", "dir");
            request.AddField("a", action);
            request.AddField("token", login.Token);
            request.AddField("dologid", login.DoLogid);
            request.AddParameter("dir_id", dir_id);
            request.AddParameter("new_name", new_name);
            if (!string.IsNullOrEmpty(to_parent_id))
            {
                request.AddParameter("to_parent_id", to_parent_id);
            }
            return request;
        }

        /// <summary>
        /// get 分享文件/取消分享 request
        /// </summary>
        /// <param name="fid">文件id</param>
        /// <param name="isShare">是否分享</param>
        /// <returns></returns>
        public static RestRequest GetShareFileRequest(string fid, bool isShare = true)
        {
            var request = new RestRequest { Method = WebMethod.Post };
            request.AddField("m", "file");
            if (isShare)
            {
                request.AddField("a", "share_file");
            }
            else
            {
                request.AddField("a", "cancel_share_file");
            }
            request.AddField("token", login.Token);
            request.AddField("dologid", login.DoLogid);
            request.AddParameter("fid", fid);
            return request;
        }

        /// <summary>
        /// get 获得回收站列表 request
        /// </summary>
        /// <param name="page_size">每页显示条数</param>
        /// <param name="page">第几页</param>
        /// <returns></returns>
        public static RestRequest GetRecycleListRequest(string page_size, string page)
        {
            var request = new RestRequest { Method = WebMethod.Post };
            request.AddField("m", "recycle");
            request.AddField("a", "get_list");
            request.AddField("token", login.Token);
            request.AddField("dologid", login.DoLogid);
            request.AddParameter("page_size", page_size);
            request.AddParameter("page", page);
            return request;
        }

        /// <summary>
        /// get 清空回收站 request
        /// </summary>
        /// <returns></returns>
        public static RestRequest GetRecycleClearRequest()
        {
            return GetRequest("truncate_recycle", "recycle");
        }

        /// <summary>
        /// get 从回收站中彻底删除一个文件 request
        /// </summary>
        /// <param name="fid">文件id</param>
        /// <returns></returns>
        public static RestRequest GetRecycleDeleteFileRequest(string fid)
        {
            return GetRecycleHandleRequest("delete_file", fid);
        }

        /// <summary>
        /// get 从回收站中彻底删除一个目录 request
        /// </summary>
        /// <param name="dir_id">目录id</param>
        /// <returns></returns>
        public static RestRequest GetRecycleDeleteDirRequest(string dir_id)
        {
            return GetRecycleHandleRequest("delete_dir", dir_id, false);
        }

        /// <summary>
        /// get 从回收站中还原一个文件 request
        /// </summary>
        /// <param name="fid">文件id</param>
        /// <returns></returns>
        public static RestRequest GetRecycleRestoreFileRequest(string fid)
        {
            return GetRecycleHandleRequest("restore_file", fid, false);
        }

        /// <summary>
        /// get 从回收站中还原一个目录 request
        /// </summary>
        /// <param name="dir_id">目录id</param>
        /// <returns></returns>
        public static RestRequest GetRecycleRestoreDirRequest(string dir_id)
        {
            return GetRecycleHandleRequest("restore_dir", dir_id, false);
        }

        internal static RestRequest GetRecycleHandleRequest(string action, string id, bool isFile = true)
        {
            var request = new RestRequest { Method = WebMethod.Post };
            request.AddField("m", "recycle");
            request.AddField("a", action);
            request.AddField("token", login.Token);
            request.AddField("dologid", login.DoLogid);
            if (isFile)
            {
                request.AddParameter("fid", id);
            }
            else
            {
                request.AddParameter("dir_id", id);
            }
            return request;
        }

        /// <summary>
        /// get 通过路径得到目录id request
        /// </summary>
        /// <param name="path">路径</param>
        /// <returns></returns>
        public static RestRequest GetDiridWithPathRequest(string path)
        {
            var request = new RestRequest { Method = WebMethod.Post };
            request.AddField("m", "dir");
            request.AddField("a", "get_dirid_with_path");
            request.AddField("token", login.Token);
            request.AddField("dologid", login.DoLogid);
            request.AddParameter("path", path);
            return request;
        }
    }
}
